package adapter;/*
 *@创建者 lnn
 *@创建时间2016/8/3122:15
 *@描述 ${TODO}
*/

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.boltshow.R;
import com.example.administrator.boltshow.viewone.FlowLayout;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import model.ShopModel;

public class ShopRecyclerAdapter extends RecyclerView.Adapter<ShopRecyclerAdapter.MyViewHolder> {
    private List<ShopModel.DataBean> mData;
    private Context mContext;
    private ImageOptions mImageOptions;
    private View.OnClickListener mListenerColor;
    private View.OnClickListener mListenerSize;

    public ShopRecyclerAdapter(List<ShopModel.DataBean> data, Context context ) {
        mData = data;
        mContext = context;
        mImageOptions = new ImageOptions.Builder().setUseMemCache(true).build();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_shop, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ShopModel.DataBean dataBean = mData.get(position);
        holder.tvFlashprice.setText("[" + dataBean.getFlashPrice() + "]");
        holder.tvName.setText(dataBean.getName());
        holder.tvCrazyPrice.setText("¥"+dataBean.getCrazyPrice());
        holder.tvCutPrice.setText(dataBean.getCutPrice());
        holder.tvDiscount.setText(dataBean.getDiscount() + "折");
        holder.tvShopPrice.setText("¥"+dataBean.getShopPrice());
        holder.tvStocknum.setText("库存" + dataBean.getStocknum());
        holder.tvBase.setText("已售" + dataBean.getBase());
        String salePoint = dataBean.getSalePoint();
        if (TextUtils.isEmpty(salePoint)) {
            holder.tvSalePoint.setVisibility(View.GONE);
            holder.viewLine1.setVisibility(View.GONE);
        } else {
            holder.tvSalePoint.setVisibility(View.VISIBLE);
            holder.viewLine1.setVisibility(View.VISIBLE);
            holder.tvSalePoint.setText(dataBean.getSalePoint());
        }

        //特别说明需要判断
        String specialNote = dataBean.getSpecialNote();
        if (TextUtils.isEmpty(specialNote)) {
            holder.tvSpecialNote.setVisibility(View.GONE);
        } else {
            holder.tvSpecialNote.setVisibility(View.VISIBLE);
            holder.tvSpecialNote.setText(specialNote);
        }
        ShopModel.DataBean.ShowBean show = dataBean.getShow();
        //判断color
        ShopModel.DataBean.ShowBean.ColorBean color = show.getColor();
        if (color == null) {
            holder.tvColor.setVisibility(View.GONE);
            holder.flColor.setVisibility(View.GONE);
        } else {
            //创建流式布局为checkbox
            holder.tvColor.setVisibility(View.VISIBLE);
            holder.flColor.setVisibility(View.VISIBLE);
            List<String> list = color.getList();
            for (int i = 0; i < list.size(); i++) {
                String s = list.get(i);
                holder.flColor.addView(creatColor(s));
            }

        }
        //判断尺寸
        ShopModel.DataBean.ShowBean.SkuSizeBean skuSize = show.getSkuSize();
        if (skuSize == null) {
            holder.tvChima.setVisibility(View.GONE);
            holder.flChima.setVisibility(View.GONE);
        } else {
            //创建流式布局
            holder.tvChima.setVisibility(View.VISIBLE);
            holder.flChima.setVisibility(View.VISIBLE);
            List<String> list = skuSize.getList();
            for (int i = 0; i < list.size(); i++) {
                String s = list.get(i);
                holder.flChima.addView(creatSize(s));
            }
        }
        x.image().bind(holder.ivSanpei, dataBean.getPayImage(), mImageOptions);
        //image
        List<ShopModel.DataBean.ImageBean> image = dataBean.getImage();
        if (image == null) {
            holder.llImagecontainer.setVisibility(View.GONE);
        } else {
            holder.llImagecontainer.setVisibility(View.VISIBLE);
            for (int i = 0; i < image.size(); i++) {
                ImageView imageView = new ImageView(mContext);
                x.image().bind(imageView, image.get(i).getImg1080url(), mImageOptions);
                holder.llImagecontainer.addView(imageView);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvFlashprice, tvName, tvCrazyPrice, tvCutPrice, tvShopPrice, tvDiscount;
        private TextView tvEmail, tvStocknum, tvBase, tvSpecialNote, tvSalePoint, tvColor, tvChima;
        private LinearLayout llImagecontainer;
        private FlowLayout flColor, flChima;
        private ImageView ivSanpei;
        private View viewLine1;

        public MyViewHolder(View view) {
            super(view);
            viewLine1 = view.findViewById(R.id.v_item_shop_line1);
            ivSanpei = (ImageView) view.findViewById(R.id.iv_item_shop_sanpei);
            tvShopPrice = (TextView) view.findViewById(R.id.tv_item_shop_shopPrice);
            tvFlashprice = (TextView) view.findViewById(R.id.tv_item_shop_flashprice);
            tvName = (TextView) view.findViewById(R.id.tv_item_shop_name);
            tvCrazyPrice = (TextView) view.findViewById(R.id.tv_item_shop_crazyPrice);
            tvCutPrice = (TextView) view.findViewById(R.id.tv_item_shop_cutPrice);
            tvDiscount = (TextView) view.findViewById(R.id.tv_item_shop_discount);
            tvEmail = (TextView) view.findViewById(R.id.tv_item_shop_email);
            tvStocknum = (TextView) view.findViewById(R.id.tv_item_shop_stocknum);
            tvBase = (TextView) view.findViewById(R.id.tv_item_shop_base);
            tvSpecialNote = (TextView) view.findViewById(R.id.tv_item_shop_specialNote);
            tvSalePoint = (TextView) view.findViewById(R.id.tv_item_shop_salePoint);
            tvColor = (TextView) view.findViewById(R.id.tv_item_shop_color);
            tvChima = (TextView) view.findViewById(R.id.tv_item_shop_chima);
            llImagecontainer = (LinearLayout) view.findViewById(R.id.ll_item_shop_imagecontainer);
            flChima = (FlowLayout) view.findViewById(R.id.fl_item_shop_chima);
            flColor = (FlowLayout) view.findViewById(R.id.fl_item_shop_color);
        }
    }


    public Button creatSize(final String str) {
        Button button = new Button(mContext);
        button.setText(str);
        button.setOnClickListener(mListenerSize);
        button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        return button;
    }

    public Button creatColor(String str) {
        Button button = new Button(mContext);
        button.setText(str);
        button.setOnClickListener(mListenerColor);
        button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        return button;
    }

    public void setOnClickListenerColor(View.OnClickListener listener) {
        mListenerColor = listener;
    }

    public void setOnClickListenerSize(View.OnClickListener listener) {
        mListenerSize = listener;
    }
}
